<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="85px"
    >
      <el-form-item label="机器人编号" prop="robotCode">
        <el-input
          v-model="queryParams.robotCode"
          placeholder="请输入机器人编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="机器人名称" prop="robotName">
        <el-input
          v-model="queryParams.robotName"
          placeholder="请输入机器人名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="健康状态" prop="robotStatus">
        <el-select
          v-model="queryParams.robotStatus"
          placeholder="请选择健康状态"
          clearable
          class="!w-240px"
        >
          <el-option label="异常" value="0" />
          <el-option label="健康" value="1" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="机器人当前电量" prop="currentPower">
        <el-input
          v-model="queryParams.currentPower"
          placeholder="请输入机器人当前电量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="主板" prop="mainBoard">
        <el-input
          v-model="queryParams.mainBoard"
          placeholder="请输入主板"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="MAC地址" prop="mac">
        <el-input
          v-model="queryParams.mac"
          placeholder="请输入MAC地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="充电次数" prop="chargingCount">
        <el-input
          v-model="queryParams.chargingCount"
          placeholder="请输入充电次数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="地点" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地点"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="区" prop="district">
        <el-input
          v-model="queryParams.district"
          placeholder="请输入区"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="处理器" prop="mainBoardCpu">
        <el-input
          v-model="queryParams.mainBoardCpu"
          placeholder="请输入处理器"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="内存" prop="mainBoardMemory">
        <el-input
          v-model="queryParams.mainBoardMemory"
          placeholder="请输入内存"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="存储设备" prop="mainBoardStorageDevice">
        <el-input
          v-model="queryParams.mainBoardStorageDevice"
          placeholder="请输入存储设备"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="处理器" prop="androidCpu">
        <el-input
          v-model="queryParams.androidCpu"
          placeholder="请输入处理器"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="内存" prop="androidMemory">
        <el-input
          v-model="queryParams.androidMemory"
          placeholder="请输入内存"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="存储设备" prop="androidStorageDevice">
        <el-input
          v-model="queryParams.androidStorageDevice"
          placeholder="请输入存储设备"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="MAC地址" prop="androidStorageMac">
        <el-input
          v-model="queryParams.androidStorageMac"
          placeholder="请输入MAC地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="麦克风" prop="microphone">
        <el-input
          v-model="queryParams.microphone"
          placeholder="请输入麦克风"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="天线" prop="antenna">
        <el-input
          v-model="queryParams.antenna"
          placeholder="请输入天线"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="雷达状态(0:异常;1:正常)" prop="radarStatus">
        <el-select
          v-model="queryParams.radarStatus"
          placeholder="请选择雷达状态(0:异常;1:正常)"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="测距模组状态(0:异常;1:正常)" prop="moduleStatus">
        <el-select
          v-model="queryParams.moduleStatus"
          placeholder="请选择测距模组状态(0:异常;1:正常)"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="超声波状态(0:异常;1:正常)" prop="waveStatus">
        <el-select
          v-model="queryParams.waveStatus"
          placeholder="请选择超声波状态(0:异常;1:正常)"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="轮毂电机状态(0:异常;1:正常)" prop="motorStatus">
        <el-select
          v-model="queryParams.motorStatus"
          placeholder="请选择轮毂电机状态(0:异常;1:正常)"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="安卓板状态(0:异常;1:正常)" prop="androidStatus">
        <el-select
          v-model="queryParams.androidStatus"
          placeholder="请选择安卓板状态(0:异常;1:正常)"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="传感器种类及规模" prop="sensorTypeScale">
        <el-input
          v-model="queryParams.sensorTypeScale"
          placeholder="请输入传感器种类及规模"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="动力系统信息" prop="powerInfo">
        <el-input
          v-model="queryParams.powerInfo"
          placeholder="请输入动力系统信息"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="检测时间" prop="heathTime">
        <el-date-picker
          v-model="queryParams.heathTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="上传时间" prop="uploadTime">
        <el-date-picker
          v-model="queryParams.uploadTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="档案编号" prop="heathCode">
        <el-input
          v-model="queryParams.heathCode"
          placeholder="请输入档案编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"
          ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
        >
        <el-button @click="resetQuery"
          ><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button
        >
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['health:robot-monitor:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['health:robot-monitor:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column label="序号" align="center" type="index" width="60px" />
      <el-table-column
        label="机器人编号"
        align="center"
        prop="robotCode"
        width="100px"
      />
      <el-table-column
        label="机器人名称"
        align="center"
        prop="robotName"
        width="100px"
      />
      <el-table-column label="健康状态 " align="center" prop="robotStatus">
        <template #default="{ row }">
          {{ robotStatusType(row.robotStatus) }}
        </template>
      </el-table-column>
      <el-table-column
        label="当前电量"
        align="center"
        prop="currentPower"
        width="100px"
      />

      <el-table-column label="充电次数" align="center" prop="chargingCount" />
      <el-table-column label="地点" align="center" prop="address" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区" align="center" prop="district" />
      <el-table-column label="处理器" align="center" prop="mainBoardCpu" />
      <el-table-column label="内存" align="center" prop="mainBoardMemory" />
      <el-table-column
        label="存储设备"
        align="center"
        prop="mainBoardStorageDevice"
      />

      <el-table-column
        label="检测时间"
        align="center"
        prop="heathTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="上传时间"
        align="center"
        prop="uploadTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="档案编号" align="center" prop="heathCode" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['health:robot-monitor:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['health:robot-monitor:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RobotMonitorForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from "@/utils/formatTime";
import download from "@/utils/download";
import { RobotMonitorApi, RobotMonitorVO } from "@/api/health/robotmonitor";
import RobotMonitorForm from "./RobotMonitorForm.vue";

/** 机器人健康档案 列表 */
defineOptions({ name: "RobotMonitor" });

const message = useMessage(); // 消息弹窗
const { t } = useI18n(); // 国际化

const loading = ref(true); // 列表的加载中
const list = ref<RobotMonitorVO[]>([]); // 列表的数据
const total = ref(0); // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  robotId: undefined,
  robotCode: undefined,
  robotName: undefined,
  robotStatus: undefined,
  currentPower: undefined,
  mainBoard: undefined,
  mac: undefined,
  chargingCount: undefined,
  address: undefined,
  province: undefined,
  city: undefined,
  district: undefined,
  mainBoardCpu: undefined,
  mainBoardMemory: undefined,
  mainBoardStorageDevice: undefined,
  androidCpu: undefined,
  androidMemory: undefined,
  androidStorageDevice: undefined,
  androidStorageMac: undefined,
  microphone: undefined,
  antenna: undefined,
  radarStatus: undefined,
  moduleStatus: undefined,
  waveStatus: undefined,
  motorStatus: undefined,
  androidStatus: undefined,
  sensorTypeScale: undefined,
  powerInfo: undefined,
  heathTime: [],
  uploadTime: [],
  heathCode: undefined,
  createTime: [],
});
const queryFormRef = ref(); // 搜索的表单
const exportLoading = ref(false); // 导出的加载中

const robotStatusType = (status) => {
  const STATUSES = [
    { robotStatus: 0, name: "异常" },
    { robotStatus: 1, name: "健康" },
  ];

  const matched = STATUSES.find((item) => item.robotStatus === status);
  return matched ? matched.name : "";
};
const StatusType = (status) => {
  const STATUSES = [
    { robotStatus: 0, name: "异常" },
    { robotStatus: 1, name: "正常" },
  ];

  const matched = STATUSES.find((item) => item.robotStatus === status);
  return matched ? matched.name : "";
};
/** 查询列表 */
const getList = async () => {
  loading.value = true;
  try {
    const data = await RobotMonitorApi.getRobotMonitorPage(queryParams);
    list.value = data.list;
    total.value = data.total;
    console.log(list.value, "list.value");
  } finally {
    loading.value = false;
  }
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields();
  handleQuery();
};

/** 添加/修改操作 */
const formRef = ref();
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id);
};

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm();
    // 发起删除
    await RobotMonitorApi.deleteRobotMonitor(id);
    message.success(t("common.delSuccess"));
    // 刷新列表
    await getList();
  } catch {}
};

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm();
    // 发起导出
    exportLoading.value = true;
    const data = await RobotMonitorApi.exportRobotMonitor(queryParams);
    download.excel(data, "机器人健康档案.xls");
  } catch {
  } finally {
    exportLoading.value = false;
  }
};

/** 初始化 **/
onMounted(() => {
  getList();
});
</script>
